from util import *
class DataResult:
    def __init__(self, stat=False, message=None, datas=None, code=None, total=None, api_link=None):
        self.stat = stat
        self.message = message or Errs.err(Code.ERROR)
        self.datas = datas
        self.code = code or Code.ERROR
        self.total = total
        self.api_link = api_link

    @classmethod
    def err(cls):
        return cls(stat=False, code=Code.ERROR)

    @classmethod
    def err_by_err_code(cls, code):
        return cls(stat=False, code=code, message=Errs.err(code))

    @classmethod
    def succ(cls):
        return cls(stat=True, code=Code.SUCCESS, message=Errs.err(Code.SUCCESS))

    @classmethod
    def success_by_message(cls, message, datas=None):
        return cls(stat=True, code=Code.SUCCESS, message=message, datas=datas)

    @classmethod
    def success_by_datas(cls, datas):
        return cls.success_by_message(Errs.err(Code.SUCCESS), datas)

    def success(self, message=None, datas=None):
        self.stat = True
        self.code = Code.SUCCESS
        self.message = message or Errs.err(Code.SUCCESS)
        self.datas = datas
        return self

    def error(self, code=None, message=None):
        self.stat = False
        self.code = code or Code.ERROR
        self.message = message or Errs.err(self.code)
        self.datas = None
        return self

    def set_api_link(self, api_link):
        self.api_link = api_link
        return self

    def to_dict(self):
        return {
            "stat": self.stat,
            "message": self.message,
            "datas": self.datas,
            "code": self.code,
            "total": self.total,
            "api_link": self.api_link,
        }
